<template>
	<view class="home">
		<navbar :isSearch="true" v-model="value" @input="change"></navbar>
		<view class="home-list">
			<view v-if="is_history" class="label-box">
				<view class="label-header">
					<text class="label-title">搜索历史</text>
					<uni-icons class="label-clear" type="trash" @click="deleteHistory"></uni-icons>					 
				</view>
				<view v-if="historyLists.length>0" class="label-content">
					<view class="label-content-items" v-for=" (item,index) in historyLists" :key="index" @click="openHistory(item)">{{item.name}}</view>
				</view>
				<view v-else class="no-data">没有搜索历史</view>				
			</view>
			<list-scroll v-else class="list-scroll">
				
				<view v-if="srarchList.length>0">
				<list-card  :item="item" v-for="item in srarchList" :key="item._id" @rememberHistory="rememberHistory"></list-card>				
				</view>
				<view v-else class="no-data">
					{{no_data}}
				</view>
			</list-scroll>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default {
		data() {
			return {
				 is_history:true,
				 srarchList:[],
				 value:'',
				 no_data:''
			}
		},
		computed:{
			...mapState(['historyLists'])
		}, 
		methods: {
			//保存历史记录
			rememberHistory(){
				this.$store.dispatch("set_history",{
					name:this.value
				})
			},
			//打开历史记录值直接查询
			openHistory(item){
				this.value=item.name
				this.is_history=false
				this.get_search(this.value)
			},
			//清空历史记录
			deleteHistory(){
				 this.$store.dispatch("delete_history_lists")
			},
			//搜索栏发生变化
			change(value){
				this.is_history=false
				//input删除为空时
				if(!value)
				{	
					clearTimeout(this.timer)
					this.mark=false					
					this.get_search(value)					
					return
				}
				
				//做个延时标记,防止频繁调用
				if(!this.mark){
					this.mark=true
					this.timer=setTimeout(()=>{
						this.mark=false
						this.get_search(value)						
						},1000)
				}
			},
			
			//查询数据
			get_search(value) {
				 this.no_data=''
				//input删除为空时,返回最初始状态
				if(!value){
				  this.srarchList=[]
				  this.is_history=true
				  return
				}
				uni.showLoading({
					title:"加载中"
				})
				this.$api.get_search({
					 value:value 
				}).then((res) => { 
					uni.hideLoading()
					const {
						data
					} = res	 
				   this.srarchList=data	 
				   this.no_data="没有搜索到相关数据"
				}).catch(()=>{
					uni.hideLoading()
				})
			} 
			
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
		background-color: #f5f5f5;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		border: 1px red solid;

		.label-box {
			background-color: #fff;
			margin-bottom: 10px;

			.label-header {
				display: flex;
				justify-content: space-between; //两端对齐
				font-size: 14px;
				color: #666;
				padding: 10px 15px;
				border-bottom: 1px #f5f5f5 solid;

				.label-title {
					color: $mk-base-color;
				}

				.label-clear {
					color: #30B33A;
					font-weight: bold; //加粗
				}
			}

			.label-content {
				display: flex;
				flex-wrap: wrap; //超出一行换行
				padding: 15px;
				padding-top: 0;

				.label-content-items {
					padding: 2px 10px;
					margin-top: 10px;
					margin-right: 10px;
					border: 1px #666 solid;
					border-radius: 5px;
					font-size: 14px;
					color: #666;
				}
			}
		}
	}

	.no-data {
		height: 200px;
		line-height: 200px;
		width: 100%;
		text-align: center;
		font-size: 14px;
		color: #666;
	}
</style>
